package cn.istartfinancial.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: Nzp
 * @date: 2018/8/22
 * @description: 处理自定义异常
 */
@Slf4j
public class CustomException implements HandlerExceptionResolver
{

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest,
                                         HttpServletResponse httpServletResponse,
                                         Object o, Exception e)
    {
        ModelAndView mv = new ModelAndView("/error/error");
        MyException myExecption = null;
        if (e instanceof MyException)
        {
            myExecption = (MyException) e;

        } else
        {
            myExecption = new MyException("未知错误");
        }

        //错误信息
        String message = myExecption.getMessage();

        ModelAndView modelAndView = new ModelAndView();

        //将错误信息传到页面
        modelAndView.addObject("message", message);

        //指向到错误界面
        return mv;
    }
}
